#include "tools.h"
#include"student.h"

bool yes_or_no(void)
{
	
	for(;;)
    {
		char cmd = getch();
		if('y' == cmd || 'Y' == cmd || 'n' == cmd || 'N' == cmd)
        {
			printf("%c\n",cmd);
			return 'y'==cmd || 'Y'==cmd;
        }
     }
}

//获取指令
int get_cmd(char start,char end)
{
    
	puts("******************");
	printf("请输入指令:");
	while(1)
	{
		int cmd =getch();
		if(start <= cmd && cmd <= end)
		{
			printf("%c\n",cmd);
			return cmd;
		}
	}
}



//获取字符串
char* get_str(char* str,size_t size)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
	assert(NULL!=str && size>1);
	size_t len;
	while(1)
	{
		len = strlen(fgets(str,size,stdin));
		if(1==len)
		{
			printf("\n输入不能为空，请重新输入：");
		}
		else
		{
			break;
		}
	}
	if('\n' == str[len-1])
	{
		str[len-1] = '\0';
	}
	else
	{
		while('\n' != getchar());
	}
	return str;
}

//延时显示
void show_msg(const char* msg,float sec)
{
	printf("\033[01;32m%s\033[00m\n",msg);
	usleep(sec*1000000);
}


//按任意键继续
void anykey_continue(void)
{
    stdin->_IO_read_ptr = stdin->_IO_read_end;
	puts("\n按任意键继续...");
	getch();
}

//得到数量
int file_num(char *NAME,char* name)
{
	char NAME_path[255];
	sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL == rfp)
		return 0;
	int num;
	fscanf(rfp,"%d",&num);
	fclose(rfp);
	return num;
}


//消除某一记录
bool del_record(char* NAME,char* name,char* record)
{
	char NAME_path[255];
	int num;
    sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL == rfp)
	{
        return false;
	}
	fscanf(rfp,"%d",&num);
	char arr[num][20];
	int cnt =-1;
	for(int i=0;i<num;i++)
	{
		fscanf(rfp,"%s",arr[i]);
		if(!strcmp(record,arr[i]))
			cnt = i;
	}
	fclose(rfp);
	if(-1 == cnt)
		return false;
	strcpy(arr[cnt],arr[num-1]);
	num--;
	
	if(num == 0)
	{
		remove(NAME_path);
		return true;
	}
	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s\n",arr[i]);
	}
	fclose(wfp);
	return true;
}

//添加一条记录
bool add_record(char* NAME,char* name,char* record)
{
	char NAME_path[255];
	int num=0;
    sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL != rfp)
	{
        fscanf(rfp,"%d",&num);	
	}
	char arr[++num][20];
	if(NULL != rfp)
		for(int i=0;i<num-1;i++)
			fscanf(rfp,"%s",arr[i]);
	strcpy(arr[num-1],record);
	if(NULL != rfp)
		fclose(rfp);

	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s\n",arr[i]);
	}
	fclose(wfp);
	return true;
}
//登入函数
bool student_login(char* id)
{
	char NAME_path[255];
	int cnt=file_num(STUDENT_PATH,id);
    if(0 == cnt)
	{	
		show_msg("账号错误,请重新输入!",1);
        return false;
	}
   	return true;
}

//删除文件
void del(char* NAME,char* name)
{
	char NAME_path[255];
    	sprintf(NAME_path,"%s%s",NAME,name);
	remove(NAME_path);
}
//写入book_isbn
bool write_book_isbn(Book_TYPE* data)
{
	char NAME_path[255];
    	sprintf(NAME_path,"%s%s",BOOK_ISBN_PATH,data->isbn);
 	FILE* wfp = fopen(NAME_path,"w");
   	if(NULL == wfp)
        	return false;
	fprintf(wfp,"%s %s %s %s %s %lf %u %u %u %u %u %d\n",
          data->name,
          data->isbn,
          data->press,
          data->author,
          data->position,
          data->price,
          data->purchase_num,
          data->now_num,
          data->stock_num,
          data->natural_damage,
          data->person_damage,
          data->off);
	fclose(wfp);
 	return true; 
}

//读出book_isbn
bool read_book_isbn(char* isbn,Book_TYPE* data)
{
	char NAME_path[255];
    	sprintf(NAME_path,"%s%s",BOOK_ISBN_PATH,isbn);
 	FILE* rfp = fopen(NAME_path,"r");
    	if(NULL == rfp)
	{	
		show_msg("查询不到该书!",1);
        	return false;
	}
	fscanf(rfp,"%s %s %s %s %s %lf %u %u %u %u %u %d",
          data->name,
          data->isbn, 
          data->press,
          data->author,
          data->position,
          &data->price,
          &data->purchase_num,
          &data->now_num,
          &data->stock_num,
          &data->natural_damage,
          &data->person_damage,
          &data->off);
    	fclose(rfp);  
	return true; 
}

//读出book_name
bool read_book_name(char* _name)
{
	int num=0;
	FILE* rfp = fopen(BOOK_ALL_PATH,"r");
	if(NULL != rfp)
        fscanf(rfp,"%d",&num);	

	char name[num][33];
	char isbn[num][20];
	for(int i=0;i<num;i++)
	{
		fscanf(rfp,"%s %s\n",name[i],isbn[i]);
		if(NULL != strstr(name[i],_name))
		{
			Book_TYPE data;
			if(!read_book_isbn(isbn[i],&data))
				printf("读取出错book/isbn/%s",isbn[i]);
			printf("%s %s %s %s\n %c区域 %c书架 第%c层 在馆数量:%u\n\n",
					data.name,data.isbn,data.press,data.author,
					data.position[0],data.position[1],
					data.position[2],data.stock_num);
		}		
	}
	fclose(rfp);
	anykey_continue();
	return true;
}

//book_order  0消除一条预约记录/1查看预约记录
bool del_order(char* isbn,const int mode)
{
	char NAME_path[255]; 
    sprintf(NAME_path,"%s%s",BOOK_ORDER_PATH,isbn);
 	FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
        return NULL;
	size_t num;
	fscanf(rfp,"%d",&num);	
	char arr[num][20];
	for(int i=0;i<num;i++)
			fscanf(rfp,"%s",arr[i]);
	if(look_order_information == mode)
	{
		for(int i=0;i<num;i++)
			printf("第%d位预约的学生是%s\n",i,arr[i]);
	}
	fclose(rfp);
	if(del_order_information == mode)
	{
		num--;
		if(0 == num)
		{
			del(BOOK_ORDER_PATH,isbn);
			return true;
		}		
		FILE* wfp = fopen(NAME_path,"w");
    	if(NULL == rfp)
        	return NULL;		
		fprintf(wfp,"%d\n",num);
		for(int i=1;i<=num;i++)
			fprintf(wfp,"%s",arr[i]);
		fclose(wfp);
		if(!del_record(STUDENT_ORDER_PATH,arr[0],isbn))
			return false;
	}		
	return true;
}

//获取当前时间
Time get_time(void)
{
	struct tm* t0 ;
	Time t1;
	time_t* t = malloc(sizeof(time_t));
	time(t);
	t0 = gmtime(t);
	free(t);
	t1.year = t0->tm_year;
	t1.mon = t0->tm_mon;
	t1.day = t0->tm_mday;
	return t1;
}
